#ifndef OS_TIMER_H
#define OS_TIMER_H

#include "os_cfg.h"
#include "os_event.h"

struct _os_timer_t;
typedef void (*os_timer_func_t) (struct _os_timer_t * timer, void * arg);

#define OS_TIMER_STARTED            (1 << 8)
#define OS_TIMER_RUNNING            (1 << 9)
#define OS_TIMER_PERIOD             (0 << 10)
#define OS_TIMER_ONE_SHOT           (1 << 10)

/**
 * 定时器
 */
typedef struct _os_timer_t {
    char name[OS_TIMER_NAME_SIZE];  // 名称

    uint16_t reload_ms;           // 超时时间
    uint16_t curr_ms;             // 当前时间
    int state;                  // 定时器状态

    os_timer_func_t func;       // 定时回调函数
    void * arg;                 // 回调参数

    os_list_item_t item;        // 链接结点
}os_timer_t;

void os_timer_service_init (void);

os_err_t os_timer_init (os_timer_t * timer, const char * name, int tmo_ms, os_timer_func_t func, void * arg, int flags);
os_err_t os_timer_uninit(os_timer_t * timer);
os_timer_t * os_timer_create (const char * name, int tmo_ms, os_timer_func_t func, void * arg, int flags);
os_err_t os_timer_free (os_timer_t * timer);
os_err_t os_timer_start (os_timer_t * timer);
os_err_t os_timer_stop (os_timer_t * timer);
int os_timer_left (os_timer_t * timer);

int os_timer_started (os_timer_t * timer);

void os_timer_tick (void);

#endif /* TTIMER_H */
